import java.util.Random;
import java.util.concurrent.LinkedBlockingDeque;

public class PCModel {
    //阻塞队列
    private static LinkedBlockingDeque<Integer>queue = new LinkedBlockingDeque<>();

    public static void main(String[] args) {
        //生产者
        Thread provider = new Thread(()->{
            Random random = new Random();
            for (int i = 0; i < 5; i++) {
                try {
                    int i1 = random.nextInt();
                    //阻塞直到插入数据
                    queue.put(i1);
                    System.out.println("生产数据:"+i1);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        //消费者
        Thread consumer = new Thread(()->{
            Integer data;
            for (int i = 0; i < 5; i++) {
                //阻塞直到取出数据
                try {
                    data = queue.take();
                    System.out.println("消费数据:"+data);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        provider.start();
        consumer.start();
    }
}
